package com.zenithmind.task.controller;

import com.zenithmind.common.context.UserContext;
import com.zenithmind.common.result.PageResult;
import com.zenithmind.common.result.Result;
import com.zenithmind.task.pojo.entity.TaskCategory;
import com.zenithmind.task.pojo.query.TaskCategoryQuery;
import com.zenithmind.task.pojo.vo.TaskCategoryVO;
import com.zenithmind.task.service.TaskCategoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import java.util.List;

/**
 * 任务分类控制器
 * 
 * @author ZenithMind Team
 * @since 2025-01-09
 */
@Slf4j
@RestController
@RequestMapping("/api/zenithMind/task/category")
@RequiredArgsConstructor
@Tag(name = "任务分类管理", description = "任务分类的创建、编辑、删除等功能")
public class TaskCategoryController {

    private final TaskCategoryService taskCategoryService;

    @GetMapping("/page")
    @Operation(summary = "分页查询分类列表", description = "支持多条件筛选的分类分页查询")
    public Result<PageResult<TaskCategoryVO>> getCategoryPage(TaskCategoryQuery query) {
        String userId = UserContext.requireUserId();
        query.setCreatorId(userId);
        PageResult<TaskCategoryVO> result = taskCategoryService.getCategoryPage(query);
        return Result.success(result);
    }

    @GetMapping("/tree")
    @Operation(summary = "查询分类树", description = "获取分类的树形结构")
    public Result<List<TaskCategoryVO>> getCategoryTree() {
        String userId = UserContext.requireUserId();
        List<TaskCategoryVO> result = taskCategoryService.getCategoryTree(userId);
        return Result.success(result);
    }

    @GetMapping("/{id}")
    @Operation(summary = "查询分类详情", description = "根据ID查询分类的详细信息")
    public Result<TaskCategoryVO> getCategoryById(@Parameter(description = "分类ID") @PathVariable String id) {
        String userId = UserContext.requireUserId();
        TaskCategoryVO category = taskCategoryService.getCategoryById(id, userId);
        return Result.success(category);
    }

    @PostMapping
    @Operation(summary = "创建分类", description = "创建新的任务分类")
    public Result<Boolean> createCategory(@Valid @RequestBody TaskCategory category) {
        String userId = UserContext.requireUserId();
        Boolean result = taskCategoryService.createCategory(category, userId);
        return Result.success(result);
    }

    @PutMapping
    @Operation(summary = "更新分类", description = "更新分类信息")
    public Result<Boolean> updateCategory(@Valid @RequestBody TaskCategory category) {
        String userId = UserContext.requireUserId();
        Boolean result = taskCategoryService.updateCategory(category, userId);
        return Result.success(result);
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除分类", description = "删除指定的分类")
    public Result<Boolean> deleteCategory(@Parameter(description = "分类ID") @PathVariable String id) {
        String userId = UserContext.requireUserId();
        Boolean result = taskCategoryService.deleteCategory(id, userId);
        return Result.success(result);
    }

    @PutMapping("/{id}/move")
    @Operation(summary = "移动分类", description = "将分类移动到新的父分类下")
    public Result<Boolean> moveCategory(
            @Parameter(description = "分类ID") @PathVariable String id,
            @Parameter(description = "新父分类ID") @RequestParam String newParentId) {
        String userId = UserContext.requireUserId();
        Boolean result = taskCategoryService.moveCategory(id, newParentId, userId);
        return Result.success(result);
    }

    @GetMapping("/user")
    @Operation(summary = "查询用户分类", description = "查询当前用户可见的分类列表")
    public Result<List<TaskCategoryVO>> getUserCategories() {
        String userId = UserContext.requireUserId();
        List<TaskCategoryVO> result = taskCategoryService.getUserCategories(userId);
        return Result.success(result);
    }

    @GetMapping("/system")
    @Operation(summary = "查询系统分类", description = "查询系统默认分类")
    public Result<List<TaskCategoryVO>> getSystemCategories() {
        List<TaskCategoryVO> result = taskCategoryService.getSystemCategories();
        return Result.success(result);
    }
}
